package com.isoftstone.project.device.area.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.isoftstone.project.device.area.domain.UArea;
import com.isoftstone.project.device.area.service.IUAreaService;
import com.isoftstone.framework.controller.BaseController;
import com.isoftstone.framework.domain.Page;
import com.isoftstone.framework.domain.Tidings;
import com.isoftstone.common.utils.ExcelUtil;
import com.isoftstone.framework.domain.PageData;

/**
 * 区域管理Controller
 *
 * @author guoX
 * @date 2021-02-02
 */
@Controller
@RequestMapping("/device/area")
public class UAreaController extends BaseController {
    private String prefix = "device/area";

    @Autowired
    private IUAreaService uAreaService;

    @RequiresPermissions("device:area:view")
    @GetMapping()
    public String area() {
        return prefix + "/area_list";
    }

    /**
     * 查询区域管理列表
     */
    @RequiresPermissions("device:area:list")
    @PostMapping("/uArealistPage")
    @ResponseBody
    public Object getuArealistPage(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        List<UArea> list = uAreaService.getUArealistPage(page);
        return Tidings.success(page.getTotalResult(), list);
    }


    /**
     * 导出区域管理列表
     */
    @RequiresPermissions("device:area:export")
    @PostMapping("/exportUArea")
    @ResponseBody
    public Object exportUArea(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        page.setEntityOrField(false);
        List<UArea> list = uAreaService.getUArealistPage(page);
        ExcelUtil<UArea> util = new ExcelUtil<UArea>(UArea. class);
        return util.exportExcel(list, "area");
    }

    /**
     * 新增区域管理
     */
    @GetMapping("/addUArea")
    public String addUArea() {
        return prefix + "/area_add";
    }


    /**
     * 新增保存区域管理
     */
    @RequiresPermissions("device:area:add")
    @PostMapping("/addUArea")
    @ResponseBody
    public Object insertUArea(UArea uArea) {
        uAreaService.insertUArea(uArea);
        return Tidings.success();

    }

    /**
     * 修改区域管理
     */
    @GetMapping("/toEditUArea")
    public String toEditUArea(Long id, ModelMap mmap) {
        UArea uArea =uAreaService.getUAreaById(id);
        mmap.put("uArea", uArea);
        return prefix + "/area_edit";
    }

    /**
     * 修改保存区域管理
     */
    @RequiresPermissions("device:area:edit")
    @PostMapping("/updateUArea")
    @ResponseBody
    public Object updateUArea(UArea uArea) {
        uAreaService.updateUArea(uArea);
        return Tidings.success();
    }

    /**
     * 删除区域管理
     */
    @RequiresPermissions("device:area:remove")
    @PostMapping("/deleteUArea")
    @ResponseBody
    public Object deleteUArea(String id) {
        uAreaService.deleteUAreaByIds(id);
        return Tidings.success();
    }

}
